package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.SuperSearchBean2;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class ChargeCenterAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<SuperSearchBean2.DataBean.TbkDgMaterialOptionalResponseBean.ResultListBean.MapDataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private OnItemClickListener mItemClickListener;
    private String maType;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public ChargeCenterAdapter2(Context context, List<SuperSearchBean2.DataBean.TbkDgMaterialOptionalResponseBean.ResultListBean.MapDataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        maType = sp.getString("SpareTwo", "");
    }

    public void setDataList(List<SuperSearchBean2.DataBean.TbkDgMaterialOptionalResponseBean.ResultListBean.MapDataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            itemView = layoutInflater.from(context).inflate(R.layout.product_list_item, parent, false);
            itemView.setOnClickListener(this);
            return new MyViewHolder(itemView);
        } else if (viewType == TYPE_FOOTER) {
            itemView = layoutInflater.from(context).inflate(R.layout.item_footer, parent,
                    false);
            return new FootViewHolder(itemView);
        }
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myviewHolder = (MyViewHolder) holder;
            myviewHolder.BindView(holder, position);
            myviewHolder.itemView.setTag(position);
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footerViewHolder = (FootViewHolder) holder;
            switch (mLoadMoreStatus) {
                case LOADING_MORE:
                    footerViewHolder.mTvLoadText.setText("正加载更多...");
                    break;
                case NO_LOAD_MORE:
                    footerViewHolder.mTvLoadText.setVisibility(View.GONE);
                    footerViewHolder.progressBar.setVisibility(View.GONE);
                    break;
                    default:
                        break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView beforePrice;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.source)
        ImageView source;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            DecimalFormat df = new DecimalFormat("0.00");
            ((MyViewHolder) holder).beforePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            if (dataList.get(position).getUser_type().equals("1")) {
                source.setImageResource(R.mipmap.tm);
            } else {
                source.setImageResource(R.mipmap.tb);
            }
            String quan = null;
            String man;
            if (dataList.get(position).getCoupon_info().equals("")) {
                quan = "0";
                man = "0";
            } else {
                quan = dataList.get(position).getCoupon_info().split("元")[1].replace("减", "").replace(".00", "");
                man = dataList.get(position).getCoupon_info().substring(1, dataList.get(position).getCoupon_info().lastIndexOf("减")).replace("元", "");
                if (Double.parseDouble(man) > Double.parseDouble(dataList.get(position).getZk_final_price())) {
                    quan = "0";
                }
            }
            ((MyViewHolder) holder).beforePrice.setText("原价" + df.format(Double.parseDouble(dataList.get(position).getZk_final_price())));
            ((MyViewHolder) holder).name.setText(dataList.get(position).getTitle());
            ((MyViewHolder) holder).num.setText("月销:" + dataList.get(position).getVolume());
            ((MyViewHolder) holder).price.setText(df.format(Double.parseDouble(dataList.get(position).getZk_final_price()) - Double.parseDouble(quan)));
            Double high = Double.parseDouble(dataList.get(position).getCommission_rate());
            ((MyViewHolder) holder).quan.setText("券  " + quan + "元");
            if (maType.equals("") || maType.equals("1")) {
                earn.setVisibility(View.GONE);
                price.setTextColor(Color.RED);
                SpannableString spannableString = new SpannableString(price.getText().toString());
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
                spannableString.setSpan(sizeSpan, 0, price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                price.setText(spannableString);
            } else {
                earn.setVisibility(View.VISIBLE);
                if (maType.equals("0")) {
                    ((MyViewHolder) holder).earn.setText("赚  " + Double.parseDouble(df.format(Double.parseDouble(price.getText().toString()) * high / 10000)) + "元");
                } else {
                    ((MyViewHolder) holder).earn.setText("赚  " + df.format(Double.parseDouble(df.format(Double.parseDouble(price.getText().toString()) * high / 10000)) / 2) + "元");
                }
                SpannableString spannableString = new SpannableString(earn.getText().toString());
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
                spannableString.setSpan(sizeSpan, 0, earn.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                earn.setText(spannableString);
            }
            RequestOptions options = new RequestOptions().override(310, 310).placeholder(R.mipmap.ploder).diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(context).load(dataList.get(position).getPict_url()).apply(options).into(product);
        }
    }

    public static class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.loading)
        TextView mTvLoadText;
        @Bind(R.id.progressBar)
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 更新加载更多状态
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }
}

